Home:ALL Converter>How to insert values into a postgresql database with serial id using sqlalchemy

How to insert values into a postgresql database with serial id using sqlalchemy

Ask Time:2021-06-26T12:05:13         Author:Viktor Avdulov

Json Formatter

I have a function that I use to update tables in PostgreSQL. It works great to avoid duplicate insertions by creating a temp table and dropping it upon completion. However, I have a few tables with serial ids and I have to pass the serial id in a column. Otherwise, I get an error that the keys are missing. How can I insert values in those tables and have the serial key get assigned automatically? I would prefer to modify the function below if possible.

def export_to_sql(df, table_name):
    from sqlalchemy import create_engine
    engine = create_engine(f'postgresql://{user}:{password}@{host}:5432/{user}')
    df.to_sql(con=engine,
              name='temporary_table',
              if_exists='append',
              index=False,
              method = 'multi')
    with engine.begin() as cnx:
        insert_sql = f'INSERT INTO {table_name} (SELECT * FROM temporary_table) ON CONFLICT DO NOTHING; DROP TABLE temporary_table'
        cnx.execute(insert_sql)

code used to create the tables

CREATE TABLE symbols
(
 symbol_id serial NOT NULL,
 symbol    varchar(50) NOT NULL,
 CONSTRAINT PK_symbols PRIMARY KEY ( symbol_id )
);


CREATE TABLE tweet_symols(
    tweet_id  varchar(50)  REFERENCES tweets,
    symbol_id   int  REFERENCES symbols,
    PRIMARY KEY (tweet_id, symbol_id),
    UNIQUE (tweet_id, symbol_id)
);

CREATE TABLE hashtags
(
 hashtag_id serial NOT NULL,
 hashtag    varchar(140) NOT NULL,
 CONSTRAINT PK_hashtags PRIMARY KEY ( hashtag_id )
);


CREATE TABLE tweet_hashtags
(
 tweet_id   varchar(50) NOT NULL,
 hashtag_id integer NOT NULL,
 CONSTRAINT FK_344 FOREIGN KEY ( tweet_id ) REFERENCES tweets ( tweet_id )
);

CREATE INDEX fkIdx_345 ON tweet_hashtags
(
 tweet_id
);

example of the tables

Author:Viktor Avdulov,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68139126/how-to-insert-values-into-a-postgresql-database-with-serial-id-using-sqlalchemy
yy